﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MT.CustomUserControl.Models;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;

namespace MT.CustomUserControl.ViewModels
{
    public class MultiComboxUcViewModel:ObservableObject
    {
        /// <summary>
        /// 是否显示
        /// </summary>
        private Visibility ucVisibility = Visibility.Visible;
        public Visibility UCVisibility
        {
            get { return ucVisibility; }
            set { SetProperty(ref ucVisibility, value); }
        }

        /// <summary>
        /// 选中项的文本显示
        /// </summary>
        private string selectedText = string.Empty;
        public string SelectedText
        {
            get { return selectedText; }
            set { SetProperty(ref selectedText, value); }
        }

        /// <summary>
        /// Combox要显示项的集合
        /// </summary>
        private ObservableCollection<MultiComboxItemModel> multiComboxItems = new ObservableCollection<MultiComboxItemModel>();
        public ObservableCollection<MultiComboxItemModel> MultiComboxItems
        {
            get { return multiComboxItems; }
            set { SetProperty(ref multiComboxItems, value); }
        }

        public MultiComboxUcViewModel()
        {
            MultiComboxItems.CollectionChanged += MultiComboxItems_CollectionChanged;
        }

        public void InitAndReset()
        {
            SelectedText = string.Empty;
            foreach (var item in MultiComboxItems)
            {
                item.IsChecked = false;
            }
        }

        private void MultiComboxItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.OldItems != null)
            {
                foreach (MultiComboxItemModel temp in e.OldItems)
                {
                    temp.PropertyChanged -= ItemPropertyChanged;
                }
            }

            if (e.NewItems != null)
            {
                foreach (MultiComboxItemModel temp in e.NewItems)
                {
                    temp.PropertyChanged += ItemPropertyChanged;
                }
            }
        }

        private void ItemPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsChecked")
            {
                MultiComboxItemModel itemModel = sender as MultiComboxItemModel;

                if (itemModel != null)
                {
                    IEnumerable<MultiComboxItemModel> items = MultiComboxItems.Where(b => b.IsChecked == true);

                    StringBuilder builder = new StringBuilder();

                    foreach (MultiComboxItemModel item in items)
                    {
                        builder.Append(item.ItemContent + "-");
                    }

                    if (builder != null)
                    {
                        SelectedText = builder.ToString();

                        if (SelectedText.Length > 1)
                            SelectedText = SelectedText.Remove(SelectedText.Length - 1);
                    }
                    else
                    {
                        SelectedText = string.Empty;
                    }
                }
            }
        }

    }
}
